package com.example.grab_the_order;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.Serializable;
import java.util.Map;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

    private BroadcastReceiver broadcastReceiver;
    // 【核心】定义 MethodChannel 的名称，必须与Dart端一致
    private static final String CHANNEL_NAME = "com.example.grab_the_order/bridge";
    private MethodChannel methodChannel;

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);

        // 【核心】初始化 MethodChannel
        methodChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL_NAME);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createNotificationChannel();

        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(GrabOrderAccessibilityService.ACTION_ORDER_DETECTED)) {
                    Serializable data = intent.getSerializableExtra(GrabOrderAccessibilityService.EXTRA_ORDER_DATA);
                    if (data instanceof Map) {
                        // 【核心】不再调用 BackgroundService，而是通过 MethodChannel 调用Dart方法
                        if (methodChannel != null) {
                            methodChannel.invokeMethod("dispatchOrderData", data);
                        }
                    }
                }
            }
        };

        IntentFilter filter = new IntentFilter(GrabOrderAccessibilityService.ACTION_ORDER_DETECTED);
        registerReceiver(broadcastReceiver, filter);
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "grab_order_channel";
            String channelName = "抢单服务";
            String channelDescription = "用于在后台运行抢单任务";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
            channel.setDescription(channelDescription);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
    }

    @Override
    protected void onDestroy() {
        if (broadcastReceiver != null) {
            unregisterReceiver(broadcastReceiver);
            broadcastReceiver = null;
        }
        super.onDestroy();
    }
}